package com.batchOperation.excel.executor.serviceImpl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.batchOperation.Template.TemplateService;
import com.batchOperation.Template.model.BatchOperationTemplate;
import com.batchOperation.excel.OperationRecord.OperationRecord;
import com.batchOperation.excel.OperationRecord.model.Record;
import com.batchOperation.excel.executor.AbstractExecutor;
import com.batchOperation.excel.executor.ExecutorInfo;
import com.batchOperation.excel.executor.IExecutor;
import com.batchOperation.excel.listener.ObjectListener;
import com.batchOperation.excel.model.Employee;
import com.batchOperation.excel.model.SchoolInfo;
import com.batchOperation.excel.support.SpringHolder;
import com.batchOperation.tool.Response;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.*;

/**
 * @Package: com.batchOperation.Executor.serviceImpl
 * @ClassName: EmployeeExecutor
 * @Author: wulongkai
 * @CreateTime: 2024/7/19 10:28
 * @Description:
 */
@Service("Employee")
@Slf4j
public class EmployeeExecutor extends AbstractExecutor implements IExecutor {

	@Autowired
	private TemplateService templateService;

	private Gson gson = new Gson();

	public Response executeV2(ExecutorInfo executorInfo) {
		Long executorId = getExecutorId();
		BatchOperationTemplate templateInfo = templateService.getTemplateInfo(executorInfo.getTemplateId());
		List<Employee> employeeList = new ArrayList<>();
		List<SchoolInfo> schoolInfoList = new ArrayList<>();
		for (int i = 0; i < templateInfo.getEntityList().size(); i++) {
			String entity = templateInfo.getEntityList().get(i);
			if (Objects.equals(entity, Employee.class.getSimpleName())) {
				employeeList = getSheetData(Employee.class, executorId, i, executorInfo.getFileName(), templateInfo);
			}
			if (Objects.equals(entity, SchoolInfo.class.getSimpleName())) {
				schoolInfoList = getSheetData(SchoolInfo.class, executorId, i, executorInfo.getFileName(), templateInfo);
			}
		}

		log.info("executorId = {}", executorId);
		log.info("employeeList = {}", gson.toJson(employeeList));
		log.info("schoolInfoList = {}", gson.toJson(schoolInfoList));

		// 自定义校验
		check(executorId, employeeList, schoolInfoList);
		System.out.println("----");

		// 处理业务逻辑
		log.info("executorId = {}", executorId);
		log.info("employeeList = {}", gson.toJson(employeeList));
		log.info("schoolInfoList = {}", gson.toJson(schoolInfoList));

		// 生成错误信息
		Response<Map<String, List<Record>>> report = SpringHolder.getBean(OperationRecord.class).report(executorId);
		final String json = gson.toJson(report.getData());
		log.info("error json:{}", json);

		return Response.getSuccessResult();
	}

	@Override
	public Response execute(ExecutorInfo executorInfo) {
		Long executorId = getExecutorId();
		BatchOperationTemplate templateInfo = templateService.getTemplateInfo(executorInfo.getTemplateId());
		List<Employee> employeeList = new ArrayList<>();
		List<SchoolInfo> schoolInfoList = new ArrayList<>();
		List<ReadSheet> sheetReadList = new ArrayList<>();
		for (int i = 0; i < templateInfo.getEntityList().size(); i++) {
			if (Objects.equals(templateInfo.getEntityList().get(i), Employee.class.getSimpleName())) {
				ObjectListener objectListener = new ObjectListener(executorId);
				ReadSheet readSheet = EasyExcel.readSheet(i)
						.head(Employee.class)
						.headRowNumber(getHeadNumber(Employee.class, templateInfo))
						.registerReadListener(objectListener)
						.build();
				sheetReadList.add(readSheet);
				employeeList = objectListener.getList();
			}
			if (Objects.equals(templateInfo.getEntityList().get(i), SchoolInfo.class.getSimpleName())) {
				ObjectListener objectListener = new ObjectListener(executorId);
				ReadSheet readSheet = EasyExcel.readSheet(i)
						.head(SchoolInfo.class)
						.headRowNumber(getHeadNumber(SchoolInfo.class, templateInfo))
						.registerReadListener(objectListener)
						.build();
				sheetReadList.add(readSheet);
				schoolInfoList = objectListener.getList();
			}
		}
		FileInputStream inputStream = getInputStream(executorInfo.getFileName());
		EasyExcel.read(inputStream).build().read(sheetReadList);

		log.info("executorId = {}", executorId);
		log.info("employeeList = {}", gson.toJson(employeeList));
		log.info("schoolInfoList = {}", gson.toJson(schoolInfoList));

		// 自定义校验
		check(executorId, employeeList, schoolInfoList);
		System.out.println("----");

		// 处理业务逻辑
		log.info("executorId = {}", executorId);
		log.info("employeeList = {}", gson.toJson(employeeList));
		log.info("schoolInfoList = {}", gson.toJson(schoolInfoList));

		// 生成错误信息
		Response<Map<String, List<Record>>> report = SpringHolder.getBean(OperationRecord.class).report(executorId);
		final String json = gson.toJson(report.getData());
		log.info("error json:{}", json);

		return Response.getSuccessResult();
	}

	public void check(Long executorId, List<Employee> employeeList, List<SchoolInfo> schoolInfoList) {
		Iterator<Employee> iterator = employeeList.iterator();
		while (iterator.hasNext()) {
			Employee employee = iterator.next();
			if (employee.getUserName().equals("wulongkai")) {
				SpringHolder.getBean(OperationRecord.class).save(executorId, employee.getRowIndex(), employee.getClass().getSimpleName(), "不允许英文名称");
				iterator.remove();
			}
		}
		Iterator<SchoolInfo> iterator2 = schoolInfoList.iterator();
		while (iterator2.hasNext()) {
			SchoolInfo schoolInfo = iterator2.next();
			if (schoolInfo.getSchoolCity().equals("guangzhou")) {
				SpringHolder.getBean(OperationRecord.class).save(executorId, schoolInfo.getRowIndex(), schoolInfo.getClass().getSimpleName(), "不允许英文名称");
				iterator2.remove();
			}
		}
	}

}
